package com.kedacom.freedomLabel.viewmodel;

import android.support.annotation.Nullable;
import android.util.Log;

import com.kedacom.freedomLabel.AppExecutors;
import com.kedacom.freedomLabel.Options;
import com.kedacom.freedomLabel.base.BaseViewModel;
import com.kedacom.freedomLabel.initdata.bean.EventBusMessage;
import com.kedacom.freedomLabel.initdata.bean.Post;
import com.kedacom.freedomLabel.initdata.bean.RubbishBean;
import com.kedacom.freedomLabel.model.bean.Business;
import com.kedacom.freedomLabel.model.bean.Config;
import com.kedacom.freedomLabel.model.db.AppDatabase;
import com.kedacom.freedomLabel.model.db.DataCallback;
import com.kedacom.freedomLabel.model.db.DataLoader;
import com.kedacom.freedomLabel.model.db.dao.ConfigDao;
import com.kedacom.freedomLabel.utils.log.LLog;
import com.kedacom.lego.message.LegoMessageManager;

import java.util.ArrayList;
import java.util.List;

public class ScaleRecordViewModel extends BaseViewModel {

//    public void queryAllRecord(long appStartTime) {
//        AppExecutors.getInstance().queryIO()
//                .submit(() -> {
//                    List<Business> businesses = AppDatabase.getInstance().businessDao()
//                            .getBusinessWithStartTime(appStartTime);
//                    sendMessage("getBusinessSuccess",businesses);
//                });
//    }
    public void queryAllRecord(long appStartTime) {
        AppExecutors.getInstance().queryIO()
                .submit(() -> {
                    List<RubbishBean> rubbishBeanList = AppDatabase.getInstance().getRubbishDao()
                            .getRubbishWithStartTime(appStartTime);
                    sendMessage("getBusinessSuccess",rubbishBeanList);
                });
    }

    public void clearAll() {
        ConfigDao dao = AppDatabase.getInstance().configDao();
        DataLoader.get(dao, "app_start_time", new DataCallback<List<Config>>() {
            @Override
            public void success(@Nullable List<Config> data) {
                try {
                    if (data != null && data.size() > 0) {
                        AppExecutors.getInstance().queryIO()
                                .submit(()-> AppDatabase.getInstance().getRubbishDao().deleteWithStartTime(Long.parseLong(data.get(0).configValue)))  ;

                        LegoMessageManager.getInstance().sendMessage(Options.MSG_POST_SUCCESS, new EventBusMessage(6, "总计页面更新数据", "总计页面", 1));


                    }
                } catch (NumberFormatException e) {
                    LLog.error(Log.getStackTraceString(e));
                }
            }

            @Override
            public void error(@Nullable String errorMessage) {
                showToast(errorMessage, TOAST_ERROR);
            }
        });
    }
    public void deleteOne(RubbishBean rubbishBean) {

        AppExecutors.getInstance().queryIO()
                .submit(() -> {
                    AppDatabase.getInstance().getRubbishDao()
                            .delete(rubbishBean);
                    LegoMessageManager.getInstance().sendMessage(Options.MSG_OTHER_SUCCESS, new EventBusMessage(7, "删除成功", "删除一个数据", 1));

                    LegoMessageManager.getInstance().sendMessage(Options.MSG_POST_SUCCESS, new EventBusMessage(6, "总计页面更新数据", "总计页面", 1));

                });


    }
//    public void clearAll() {
//        ConfigDao dao = AppDatabase.getInstance().configDao();
//        DataLoader.get(dao, "app_start_time", new DataCallback<List<Config>>() {
//            @Override
//            public void success(@Nullable List<Config> data) {
//                try {
//                    if (data != null && data.size() > 0) {
//                        AppExecutors.getInstance().queryIO()
//                                .submit(()-> AppDatabase.getInstance().businessDao().deleteWithStartTime(Long.parseLong(data.get(0).configValue)))  ;
//                    }
//                } catch (NumberFormatException e) {
//                    LLog.error(Log.getStackTraceString(e));
//                }
//            }
//
//            @Override
//            public void error(@Nullable String errorMessage) {
//                showToast(errorMessage, TOAST_ERROR);
//            }
//        });
//    }
}
